﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace DoorAndWindow
{
	public partial class DlgTopBottomDim : System.Windows.Forms.Form
	{
		public static DlgTopBottomDim GetInstance(ExternalCommandData cmd)
		{
			if (DlgTopBottomDim.ded == null || DlgTopBottomDim.ded.IsDisposed)
			{
				DlgTopBottomDim.ded = new DlgTopBottomDim(cmd);
			}
			return DlgTopBottomDim.ded;
		}

		public bool TopDim
		{
			get
			{
				return this.cbxTop.Checked;
			}
		}

		public bool BottomDim
		{
			get
			{
				return this.cbxBottom.Checked;
			}
		}

		public int SelectMode
		{
			get
			{
				if (this.rbSingle.Checked)
				{
					return 1;
				}
				return 2;
			}
		}

		public bool OK { get; set; }

		public int mode
		{
			get
			{
				return this.m_mode;
			}
		}

		public bool clickModeBtn
		{
			get
			{
				return this.m_clickModeBtn;
			}
			set
			{
				this.m_clickModeBtn = value;
			}
		}

		public DlgTopBottomDim(ExternalCommandData cmdData)
		{
			this.InitializeComponent();
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			this.RefreshType();
			base.StartPosition = FormStartPosition.Manual;
			if (!this.ReadData())
			{
				base.Location = RevitVersionFuncs.GetRevitTopLeftPoint(cmdData);
				this.cbxStyle.SelectedIndex = 0;
				this.m_mode = 0;
				this.cbxTop.Checked = true;
				this.cbxBottom.Checked = true;
				this.rbSingle.Checked = true;
				this.rbRect.Checked = false;
			}
		}

		private void RefreshType()
		{
			this.cbxStyle.Items.Clear();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			filteredElementCollector.OfClass(typeof(SpotDimensionType));
			if (filteredElementCollector != null)
			{
				IList<Element> list = filteredElementCollector.ToElements();
				foreach (Element element in list)
				{
					Parameter parameter = element.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME);
					if (parameter.AsString() == "Spot Elevations" || parameter.AsString() == "高程点")
					{
						this.cbxStyle.Items.Add(new ShowType(element as SpotDimensionType));
					}
				}
			}
			if (this.cbxStyle.Items.Count > 0)
			{
				this.cbxStyle.SelectedIndex = 0;
				this.SpotDimensionType = ((ShowType)this.cbxStyle.SelectedItem).Type;
			}
		}

		public SpotDimensionType SpotDimensionType { get; set; }

		private void cbxStyle_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.SpotDimensionType = ((ShowType)this.cbxStyle.SelectedItem).Type;
		}

		private void SendESC()
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgTopBottomDim.keybd_event(bVk, 0, dwFlags, 0);
			DlgTopBottomDim.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void DlgTopBottomDim_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void KeyEsc()
		{
			byte bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			DlgTopBottomDim.SendMessage(CmdTopBottomDim._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			DlgTopBottomDim.keybd_event(bVk, 0, dwFlags, 0);
			DlgTopBottomDim.keybd_event(bVk, 0, dwFlags2, 0);
		}

		private void DlgTopBottomDim_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				DlgTopBottomDim.SendMessage(CmdTopBottomDim._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void DlgTopBottomDim_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.KeyEsc();
		}

		private void rbSingle_CheckedChanged(object sender, EventArgs e)
		{
			this.m_mode = 0;
			this.m_clickModeBtn = true;
			this.KeyEsc();
		}

		private void rbRect_CheckedChanged(object sender, EventArgs e)
		{
			this.m_mode = 1;
		}

		private void SaveData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "TopBottomDim.dat";
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.m_mode);
			streamWriter.WriteLine(this.cbxStyle.Text);
			streamWriter.WriteLine(this.cbxTop.Checked);
			streamWriter.WriteLine(this.cbxBottom.Checked);
			streamWriter.WriteLine(this.rbSingle.Checked);
			streamWriter.WriteLine(this.rbRect.Checked);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ReadData()
		{
			string tempPath = Path.GetTempPath();
			string path = tempPath + "TopBottomDim.dat";
			if (!File.Exists(path))
			{
				return false;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new System.Drawing.Point(x, y);
				string text = streamReader.ReadLine();
				this.m_mode = int.Parse(text);
				text = streamReader.ReadLine();
				this.cbxStyle.Text = text;
				text = streamReader.ReadLine();
				this.cbxTop.Checked = bool.Parse(text);
				text = streamReader.ReadLine();
				this.cbxBottom.Checked = bool.Parse(text);
				text = streamReader.ReadLine();
				this.rbSingle.Checked = bool.Parse(text);
				text = streamReader.ReadLine();
				this.rbRect.Checked = bool.Parse(text);
			}
			catch
			{
				return false;
			}
			finally
			{
				streamReader.Close();
				fileStream.Close();
			}
			if (this.m_mode == 0)
			{
				this.rbSingle.Checked = true;
				this.rbRect.Checked = false;
			}
			else if (1 == this.m_mode)
			{
				this.rbRect.Checked = true;
				this.rbSingle.Checked = false;
			}
			return true;
		}

		private void DlgTopBottomDim_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private static DlgTopBottomDim ded;

		private Document m_doc;

		private int m_mode;

		private bool m_clickModeBtn;
	}
}
